$(function(){
	
	//删除图片
	$('.weui-uploader__files').on("click",".delimportimg",function(){
		$(this).closest("li").remove();
	})
	//重新上传图片
	$('.weui-uploader__files').on("click",".base64_refresh",function(){
		var element=$(this).closest("li");
		if(element.closest('.weui-uploader__bd').find("input").attr("id")=='uploaderInput'){
			if($('#uploaderInput').attr("large_image_num")>0){
				$(".uploadoper").find("a").eq(1).attr("on","true");
				refleshUpload(element);
			}else{
				$(".dialog_tips").show().find(".dialog_txt").text("最多只能上传"+$(this).attr("image_num")+"张照片");	
				setTimeout(function(){$(".dialog_tips").hide()},1200)
			}
		}else{
			refleshUpload(element);
		}
		
	})
	//获取作品
	$('.uploadcon ').on("touchend",".uploadoper a",function(e){
		e.stopPropagation();
		if(!$(".uploadoper").find("a").eq(1).is(":hidden")&&$(".uploadoper").find("a").eq(1).attr("on")){			
			$(".dialog_tips").show().find(".dialog_txt").text("正在上传照片，请稍后...");	
			setTimeout(function(){$(".dialog_tips").hide()},1200);
			return;
		}
		$("#loadtoast").show();
		opentype=2;
		if($(".albumname").val().length>0){
			
			baseAjax('other/work/update_work','POST',{work_id:workid,work_name:$(".albumname").val()},function(data){
				
				//作品页面初始化接口 并且生成作品
				initializationwork();
			})
		}else{
			//作品页面初始化接口 并且生成作品
			initializationwork();
		}
		
	})
	
	//输入作品名
	$(".albumname").on('input',function(){	
		if($(".albumname").val().length>0){
			$(".uploadoper").find("a").eq(1).show().siblings("a").hide();
		}else{
			$(".uploadoper").find("a").eq(0).show().siblings("a").hide();
		}
	})
	
	//照片像素过低，可能导致照片模糊
	$(document).on('click','.weui-icon-warn',function(e){	
		e.stopPropagation();
		$(".dialog_tips").show().find(".dialog_txt").text("照片像素过低，可能导致照片模糊");	
		setTimeout(function(){$(".dialog_tips").hide()},1200)
	})
	
	
	
	
})

//获取当前时间；	
function getCurrenTemptime(imagename){	
		var myDate = new Date();	
		var y = myDate.getFullYear(); 
		var mo = myDate.getMonth() + 1 < 10 ? '0' + (myDate.getMonth() + 1) : (myDate.getMonth() + 1);
	    var d = myDate.getDate() < 10 ? '0' + myDate.getDate() : myDate.getDate();
	    var h = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours();
        var m = myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes();
        var s = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds();
		//var a=h+m+s;		
		return y+mo+d+h+m+s+imagename;
	
	}

//重新上传图片
function refleshUpload(element){
	var bw=boxwindthheight.width;
	var bh=boxwindthheight.height;
	var imgws=bw>bh?bh:bw;	
	imgws=imgws<imgsize?imgsize:imgws;	
	$.ajax({
				type: "POST",
				url: baseUrl+"other/image/upload_image",
				data: {image_data:element.find(".upload_img").attr("src"),user_id:user_id,source:2,size:imgws,work_id:workid},
				dataType: 'json',				
				beforeSend: function(XMLHttpRequest){
				element.find(".up_refresh").remove();
				element.find(".up_refreshtxt").remove();
				element.append('<div class="weui-uploader__file-content la-ball-beat la-sm"><i></i><i></i><i></i> </div>');
				},
				success: function(result) {
					if(result.code!=200){ 					
					element.find(".la-ball-beat").remove();
					element.append('<div class="weui-uploader__file-content up_refresh base64_refresh"> <i class="fa fa-refresh fa-lg"></i></div><div class="up_refreshtxt"><span>上传失败</span><span>重新上传</span> </div>');
					dialogshow("错误",result.description);
						
					}else{						
						element.find(".la-ball-beat").remove();					
						if(result.data.quality!=0){//照片的一边小于900    照片像素过低可能导致，图片模糊   0=正常；1=比例不符合；2=像素不符合//照片的一边小于900    照片像素过低可能导致，图片模糊
							element.append('<div class="upload_i_worn"><i class="weui-icon-warn"></i> </div>');
						}	
						item.addClass("upload_sucess");
						element.find(".upload_img").attr("src",result.data.image_url);
						element.find(".upload_img").attr("image_id",result.data.id);
						element.attr({"imgw":result.data.width,"imgh":result.data.height});
						element.removeClass("weui-uploader__file_status");
						element.append('<div class="delimportimg"><i class="fa fa-close fa-fw"></i></div>');
						
						if(element.closest('.weui-uploader__bd').find("input").attr("id")=='uploaderInput'){
							$(".upload_num").text($(".upload_sucess").length);
							$(".uploadoper").find("a").eq(1).show().siblings("a").hide();
							$('#uploaderInput').attr("large_image_num",$('#uploaderInput').attr("image_num")-parseInt($(".upload_num").text()));						
							if($("#uploaderFiles .la-ball-beat").length==0){
								$(".uploadoper").find("a").eq(1).removeAttr("on");
							}
						}
						
						
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {					
					element.find(".la-ball-beat").remove();					
					element.append('<div class="weui-uploader__file-content up_refresh base64_refresh"> <i class="fa fa-refresh fa-lg"></i></div><div class="up_refreshtxt"><span>上传失败</span><span>重新上传</span> </div>');
				
				}
			
			});
	
	
}

//作品页面初始化接口 并且生成作品
function initializationwork(){
	baseAjax('other/work/init_work','GET',{work_id:workid,is_return:1},function(data){		
			UtilProperty[workid].worker=data.data;
			//ImageToObject();				
			//获取布局列表
			lay_listajax();
			//获取作品图片列表
			image_listajax();
			
			
			
		})
	
}

//单张上传图片
function getchange(file){
	if(!$("#singerFiles").hasClass('la-ball-beat')){
	var _flag=file.getAttribute('onfunction');
	var bw=boxwindthheight.width;
	var bh=boxwindthheight.height;
	var imgws=bw>bh?bh:bw;	
	imgws=imgws<imgsize?imgsize:imgws;	
	imageComporess (file, 80, function(data){
			var name= getCurrenTemptime(data.name);
			$.ajax({
				type: "POST",
				url: baseUrl+"other/image/upload_image",
				data: {image_data:data.base64url,user_id:user_id,source:2,size:imgws,work_id:workid},
				dataType: 'json',				
				beforeSend: function(XMLHttpRequest){
				var li='<li class="weui-uploader__file weui-uploader__file_status" name="'+name+'">'+
					               	'<div class="upload_img" style="background-image:url('+data.base64url+')" src="'+data.base64url+'"></div>'+
					               	'<div class="weui-uploader__file-content la-ball-beat la-sm"><i></i><i></i><i></i> </div>'+
				              	 '</li>';
				                
				$("#singerFiles").prepend(li);
				},
				success: function(result) {
					if(result.code!=200){ 
					var item=$('#singerFiles li[name="'+name+'"]')
					item.find(".la-ball-beat").remove();
					item.append('<div class="weui-uploader__file-content up_refresh base64_refresh"> <i class="fa fa-refresh fa-lg"></i></div><div class="up_refreshtxt"><span>上传失败</span><span>重新上传</span> </div>');
				
					 dialogshow("错误",result.description);
						
					}else{
						var res=result.data;
						var item=$('#singerFiles li[name="'+name+'"]')
						item.find(".la-ball-beat").remove();					
						if(res.quality!=0){//照片的一边小于900    照片像素过低可能导致，图片模糊   0=正常；1=比例不符合；2=像素不符合//照片的一边小于900    照片像素过低可能导致，图片模糊
							item.append('<div class="upload_i_worn"><i class="weui-icon-warn"></i> </div>');
						}
						item.find(".upload_img").attr("src",res.image_url);
						item.find(".upload_img").attr("image_id",res.id);
						item.attr({"imgw":res.width,"imgh":res.height});
						item.removeClass("weui-uploader__file_status");
						item.append('<div class="delimportimg"><i class="fa fa-close fa-fw"></i></div>');
						var o={height:res.height,image_id:res.id,image_url:res.image_url,status:0,user_id:res.user_id,width:res.width,work_id:workid};
						UtilProperty[workid].image_list.push(o);
						if(_flag=='0'){//在控件上添加图片						
							addimgaetocanvas(res.image_url,res.id,res.width,res.height);
						}else if(_flag=='1'){//替换替换图片							
							canvasimgreplace(res.image_url,res.id,res.width,res.height);
						}				
					
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					var item=$('#singerFiles li[name="'+name+'"]')
					item.find(".la-ball-beat").remove();
					item.append('<div class="weui-uploader__file-content up_refresh base64_refresh"> <i class="fa fa-refresh fa-lg"></i></div><div class="up_refreshtxt"><span>上传失败</span><span>重新上传</span> </div>');
				
				}
			
			});
	})
	
	}else{
		$(".dialog_tips").show().find(".dialog_txt").text("正在上传，请稍后");	
		setTimeout(function(){$(".dialog_tips").hide()},1200)
	}
	
}

